<?php
# http://projects:8081/cdapp-2kt-api/api/journal_issue_operator.php
include_once '_portal.php';

$input = $post->input;
$action = $post->action;
if (!$input || !$action) {
  die();
}

if ($action == 'create') {
  $sql = "INSERT INTO `2kt_journal_issue`(`equipment`, `eventTime`, `description`, `requirement`, `name`, `reported`, `replied`, `reply`) VALUES ('{$input->equipment}','{$input->eventTime}','{$input->description}','{$input->requirement}','{$input->name}',{$input->reported},{$input->replied},'{$input->reply}')";
  $result = exec_sql($sql);
  output(['sql' => $sql, 'result' => $result]);
} elseif ($action == 'query') {
  $equipment_list = "'" . implode("','", $input->equipments) . "'";
  $sql = "SELECT * FROM `2kt_journal_issue` WHERE `equipment` IN ({$equipment_list}) AND `eventTime` >= '{$input->start} 00:00:00' AND `eventTime` <= '{$input->end} 23:59:59' AND `status` > 0";
  $result = exec_sql($sql);
  output(['sql' => $sql, 'result' => $result]);
} elseif ($action == 'delete') {
  $delete_list = "'" . implode("','", $input) . "'";
  $sql = "UPDATE `2kt_journal_issue` SET `status` = 0 WHERE `number` IN ($delete_list)";
  $result = exec_sql($sql);
  output(['sql' => $sql, 'result' => $result]);
} elseif ($action == 'reply') {
  $sql = "UPDATE `2kt_journal_issue` SET `replied` = 1, `reply` = '{$input->reply}' WHERE `number` = {$input->number}";
  $result = exec_sql($sql);
  output(['sql' => $sql, 'result' => $result]);
} else {
  //
}
